package com.tangtang.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tangtang.gulimall.product.Vo.AtrrVo;
import com.tangtang.gulimall.product.Vo.AttrRespVo;
import com.tangtang.gulimall.product.Vo.AttrUpdateVo;
import com.tangtang.gulimall.product.entity.AttrGroupEntity;
import com.tangtang.gulimall.product.service.AttrGroupService;
import com.tangtang.gulimall.product.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tangtang.gulimall.product.entity.AttrEntity;
import com.tangtang.gulimall.product.service.AttrService;
import com.tangtang.common.utils.PageUtils;
import com.tangtang.common.utils.R;



/**
 * 商品属性
 *
 * @author tangtang
 * @email a1104707271@vip.qq.com
 * @date 2020-09-11 15:22:09
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    //查出所属分类的名称
    @Autowired
    private CategoryService categoryService;

    //查询所属分组
    private AttrGroupService attrGroupService;

    /**
     * 获取销售属性
     * @param params
     * @param catelogId
     * @return
     */
    @RequestMapping("/sale/list/{catelogId}")
    public R attrsalelist(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId)
    {
        System.out.println("有请求进入销售属性！"+catelogId);
        PageUtils page = attrService.queryAttrsale(params,catelogId);
        return R.ok().put("page",page);
    }

    /**
     * 获取规格参数
     * /product/attrgroup/list/{catelogId}
     */
    @RequestMapping("/base/list/{catelogId}")
    public R attrbaselist(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId)
    {
        PageUtils page = attrService.queryAttrbase(params,catelogId);
        return R.ok().put("page",page);
    }

    /**
     * 列表,这个是获取所有的属性信息
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		//AttrEntity attr = attrService.getById(attrId);
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", respVo);
    }

    /**
     * 保存（AttrEntity attr）
     * 使用vo类来进行保存
     * 做了两步操作，
     * 第一：保存了正常的AttrEntity实体数据
     * 第二：保存了attrAttrgroupRelationEntity关联的属性id
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AtrrVo attr){
		//attrService.save(attr);
        attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     * 原修改，只是基于实体类，AttrEntity
     * 现修改基于AttrRespVo来修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrRespVo attr){
		//attrService.updateById(attr);
        attrService.updateAttr(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
